﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Reader.Libs.PRC.Metadata
{
    public class MobiMetadata
    {
        private PDBHead pdbHeader;
        private PalmDOCHead palmDocHeader;
        private MobiHead mobiHeader;

        public PDBHead PDBHeader
        {
            get { return pdbHeader; }
        }

        public PalmDOCHead PalmDocHeader
        {
            get { return palmDocHeader; }
        }

        public MobiHead MobiHeader
        {
            get { return mobiHeader; }
        }

        public MobiMetadata()
        {
            pdbHeader = new PDBHead();
            palmDocHeader = new PalmDOCHead();
            mobiHeader = new MobiHead();
        }

        public void readMetaData(FileStream fs)
        {
            pdbHeader.readPDBHead(fs);
            palmDocHeader.readPalmDocHead(fs);
            mobiHeader.readMobiHead(fs, pdbHeader.MobiHeaderSize);
        }

        public void writeMetaData(FileStream fs, string _content)
        {
            try
            {
                byte[] content = System.Text.Encoding.UTF8.GetBytes(_content);
                int re_count = content.Length / 4096 + 1;
                int te_len = _content.Length;
                pdbHeader.writePDBHead(fs, re_count);
                palmDocHeader.writePalmDocHead(fs, te_len, re_count);
                mobiHeader.writeMobiHead(fs);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
